#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct card{
    int value;
    char* color;
    struct card *next;
} LinkList;



LinkList *creat(){
    char* color_arr[]  = {"红桃","梅花","方块","黑桃"};
    LinkList *head, *node, *end;//定义头节点，普通节点，尾部节点；
    head = (LinkList*)malloc(sizeof(LinkList));//分配地址
    end = head;         //若是空链表则头尾节点一样
    for (int i = 0; i < 13; i++) {
        for (int j = 0; j < 4; j++) {
            node = (LinkList*)malloc(sizeof(LinkList));
            node->color = color_arr[j];
            node->value = i+1;
            end->next = node;
            end = node;
        }
    }
    end->next = NULL;//结束创建
    return head;
}


int main(int argc, char *argv[])
{
    srand(time(NULL));
    printf("生成牌\n");
    LinkList *head = creat();
    LinkList *f,*h;
    h= head;
    while (h->next != NULL) {
           h = h->next;
          printf("%s%d\t",h->color,h->value);
       }
    printf("\n洗牌\n");
  	 
    int count = 52,i=0,j=0,rand_num;
    LinkList * result[4][13];
    for(i=0;i<4;i++){
        for(j=0;j<13;j++){
        	h= head;
            rand_num=(rand()%count);
            while (rand_num>1)
            {
                h = h->next;
				rand_num--; 
			}
			f=h;
			h=h->next;
			printf("\n这是第%d张: ",52-count+1);
			printf("%s",h->color);
			printf(" %d\n",h->value);
        	result[i][j]=h;
            f->next = h->next;
            count--;
//            free(h); 不能free 被使用！！！！ 
            
            h= head;
    		while (h->next != NULL) {
          	h = h->next;
           		printf("%s%d\t",h->color,h->value);
     	 	}
                
        }
    }
    for(i=0;i<4;i++){
        printf("\n第%d号选手的牌:\n",i+1);
        for(j=0;j<13;j++){
           printf("%s %d\t",result[i][j]->color,result[i][j]->value);
        }
         printf("\n");
    }
    return 0;
}

